package com.school.system.service;

import com.school.system.entity.Student;
import com.school.system.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 学生服务类
 * 
 * @author 鹿与兔
 * @since 2025-05-22
 */
@Service
public class StudentService {

    @Autowired
    private StudentRepository studentRepository;

    /**
     * 分页查询学生列表
     */
    public Page<Student> getStudentPage(Pageable pageable) {
        return studentRepository.findAll(pageable);
    }

    /**
     * 获取所有学生列表
     */
    public List<Student> getAllStudents() {
        return studentRepository.findAll();
    }

    /**
     * 根据ID获取学生信息
     */
    public Student getStudentById(Long id) {
        Optional<Student> student = studentRepository.findById(id);
        return student.orElse(null);
    }

    /**
     * 根据学号获取学生信息
     */
    public Student getStudentByStudentNo(String studentNo) {
        return studentRepository.findByStudentNo(studentNo);
    }

    /**
     * 添加学生
     */
    public Student addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setDeleted(0);
        return studentRepository.save(student);
    }

    /**
     * 更新学生信息
     */
    public Student updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        return studentRepository.save(student);
    }

    /**
     * 删除学生
     */
    public void deleteStudent(Long id) {
        studentRepository.deleteById(id);
    }

    /**
     * 根据班级ID获取学生列表
     */
    public List<Student> getStudentsByClassId(Long classId) {
        return studentRepository.findByClassId(classId);
    }

    /**
     * 根据姓名搜索学生
     */
    public List<Student> searchStudentsByName(String name) {
        return studentRepository.findByNameContaining(name);
    }

    /**
     * 根据状态获取学生列表
     */
    public List<Student> getStudentsByStatus(Integer status) {
        return studentRepository.findByStatus(status);
    }
}
